#include "vcd-common.h"
#include "vcd-conf/vcd-conf.h"
#include "vcd-download.h"
#include "vcd-conf/vcd-conf.h"
#include "vcd-vm-status-control.h"
#include "vcd-get-login-ticket.h"
#include "vcd-linux-ui/vcd-message-dialog.h"

static void start_vm_sys_callback(gpointer p);
static void resume_vm_sys_callback(gpointer p);
static void reboot_sys_callback(gpointer p);
static void stop_sys_callback(gpointer p);
static gboolean operate_vm_func(gpointer data);

static gboolean operate_vm_func(gpointer data)
{
    g_return_val_if_fail(data != NULL, FALSE);
    struct network_response *res = (struct network_response*)data;
    if (res->memory == NULL) {
        if (res->error_msg) {
            char szbuf[255];
            snprintf(szbuf, sizeof(szbuf), "操作失败:%s", res->error_msg);
            message_box(szbuf);
        }
    }
    network_response_free(&res);
    return FALSE;
}

static int operate_vm_sys(gpointer p, const char* opt)
{
    vm_info* f = (vm_info*)p;
    //https://192.168.10.221:8006/api2/json/nodes/n207/qemu/102/status/start
    struct curl_slist *list = NULL;
    char post_fields[1000] = {0};
    char url[480] = "https://";
    int ret = 0;
    if (PRIVATE_LOGIN == g_vcd_conf->login_way) {
        list = curl_slist_append(list, g_final_ticket);
        list = curl_slist_append(list, g_token);

        snprintf(url, sizeof(url),
                 "https://%s/api2/json/nodes/%s/qemu/%d/status/%s",
                 get_ip_port(), f->node, f->vmid, opt);
    } else {
//        strcpy(url, "https://api3.verycloud.cn/API/instance/update");
//        list = curl_slist_append(list, "Accept: application/json");
//        list = curl_slist_append(list, "Content-Type: application/json; charset=utf-8");
//        strcpy(post_fields, "{\"token\":\"");
//        strcat(post_fields, g_public_access_token);
//        strcat(post_fields, "\",\"id\":\"");
//        strcat(post_fields, startsystemdata.public_id);
//        strcat(post_fields, "\",\"op\":\"start\"}");
    }

    struct network_request_arg* pt = (struct network_request_arg*)malloc(sizeof(struct network_request_arg));
    g_return_val_if_fail(pt != NULL, -1);

    strncpy(pt->url, url, sizeof(pt->url));
    pt->http_method = CURLOPT_POST;
    strncpy(pt->post_fields, post_fields, sizeof(pt->post_fields));
    pt->timeout = 3;
    pt->list = list;
    pt->data_priv = NULL;
    pt->callback = operate_vm_func;

    net_req_list_add(pt);
    return ret;
}

static void start_vm_sys_callback(gpointer p)
{
    operate_vm_sys(p, "start");
}

static void resume_vm_sys_callback(gpointer p)
{
    operate_vm_sys(p, "resume");
}

static void reboot_sys_callback(gpointer p)
{
    operate_vm_sys(p, "reset");
}

static void stop_sys_callback(gpointer p)
{
    operate_vm_sys(p, "stop");
}

int start_vm_sys(vm_info* f)
{
    M_DEBUG("start vm");
    message_diag_context *t = g_malloc0(sizeof(message_diag_context));
    g_return_val_if_fail(t != NULL, -1);
    t->btn_yes = start_vm_sys_callback;
    t->btn_no = NULL;
    t->info = "设备未开启，是否开启设备？";
    t->user_data = f;
    vcd_message_dialog(t);
    g_free(t);
    t = NULL;
    return 0;
}
int resume_vm_sys(vm_info* f)
{
    M_DEBUG("resume vm");
    message_diag_context *t = g_malloc0(sizeof(message_diag_context));
    g_return_val_if_fail(t != NULL, -1);
    t->btn_yes = resume_vm_sys_callback;
    t->btn_no = NULL;
    t->info = "虚拟机挂起或暂停，是否恢复设备？";
    t->user_data = f;
    vcd_message_dialog(t);
    g_free(t);
    t = NULL;
    return 0;
}

int reboot_vm_sys(vm_info* f)
{
    M_DEBUG("reboot vm");
    message_diag_context *t = g_malloc0(sizeof(message_diag_context));
    g_return_val_if_fail(t != NULL, -1);
    t->btn_yes = reboot_sys_callback;
    t->btn_no = NULL;
    t->info = "您将要重启虚拟机！是否继续？";
    t->user_data = f;
    vcd_message_dialog(t);
    g_free(t);
    t = NULL;
    return 0;
}

int stop_vm_sys(vm_info* f)
{
    M_DEBUG("stop vm");
    message_diag_context *t = g_malloc0(sizeof(message_diag_context));
    g_return_val_if_fail(t != NULL, -1);
    t->btn_yes = stop_sys_callback;
    t->btn_no = NULL;
    t->info = "您将要强制关机并退出！是否继续？";
    t->user_data = f;
    vcd_message_dialog(t);
    g_free(t);
    t = NULL;
    return 0;
}
